<?php

require("function.php");

// Start XML file, create parent node

$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);

// Select all the rows in the markers table
$relaytype = intval($_REQUEST['relaytype']);

$query = "SELECT * FROM routers WHERE country <> ''";
if ($relaytype > 0) {
	if ($relaytype == 1) $query .= " and exitrelay = 1";
	if ($relaytype == 2) $query .= " and exitrelay = 0";
}
$result = db_fetch($query);
if (!$result) {
  die('Invalid query: ' . mysql_error());
}

header("Content-type: text/xml");

// Iterate through the rows, adding XML nodes for each

foreach ($result as $row){
  // ADD TO XML DOCUMENT NODE
  $node = $dom->createElement("marker");
  $newnode = $parnode->appendChild($node);
  $newnode->setAttribute("ipaddr",$row['ipaddr']);
  $newnode->setAttribute("hostname", $row['hostname']);
  $newnode->setAttribute("routername", $row['routername']);
  $newnode->setAttribute("uptime", getDayHour($row['uptime']));
  $newnode->setAttribute("platform", $row['platform']);
  $newnode->setAttribute("country", $row['country']);
  $newnode->setAttribute("exitrelay", $row['exitrelay'] == 1 ? "Yes": "No");
  $newnode->setAttribute("lat", $row['lat']);
  $newnode->setAttribute("lng", $row['long']);
}

echo $dom->saveXML();

function getDayHour($hour) {
	$str = "";
	if ($hour>24) {
		$str = intval($hour / 24) . " days ";
		$hour = $hour % 24; 
	}
	$str .= $hour . " hours";
	return $str;
}
?>